<?php
namespace plugin\videoappdownload\model;
use think\Model;
use think\facade\Db;
use think\model\concern\SoftDelete;
class PluginVideoappdownloadArticle extends Model {
    
 
	//软删除
	use SoftDelete;
	// protected $deleteTime = 'delete_time';
	protected $defaultSoftDelete = 0;
 
	//关联内容详情
	public function contents() {
		return $this->hasOne(PluginVideoappdownloadArticleContent::class, 'article_id')->bind(['content']);
	}
	public function addcontents() {
		return $this->hasOne(PluginVideoappdownloadArticleContent::class,'article_id', 'id');
	}
	//关联上级分类 
	public function Category() {
		return $this->belongsTo(PluginVideoappdownloadArticleCategory::class, 'category_id')->bind(['name','icon']);
	}
	//获取器分类名称
	public function getStatusAttr($value) {
		$status = [-1=>'删除',0=>'被拒绝',1=>'待审核',2=>'正常'];
		return $status[$value];
	}
	
	  public function demo()
    {
        
        
   
     $data = PluginVideoappdownloadArticle::with( 'contents'	)->select()->toArray();
  
        
        
        
        
// $user           = new PluginvideoappdownloadArticle;
// $user->title     = 'thinkphp';
// $user->create_time    = time();
// $user->save();
        
// $user = new PluginvideoappdownloadArticle;
// $user->save([
//     'title'  =>  'thinkphp1',
//     'create_time' => time()
// ]);
// $data=[
    
//     'title'  =>  'thinkphp12',
//     'create_time' => time()
// ];
        
// $user = new PluginvideoappdownloadArticle;
// // post数组中只有name和email字段会写入
// $user->allowField(['title,time'])->save($data);


 
// $user = PluginvideoappdownloadArticle::where('id',116) 
// 	->find();
  
//   $data['title']='title';
  
   
//     $user->allowField(['title', 'uid'])->save($data);

// $user = new PluginvideoappdownloadArticle;
// $list = [
//     ['id'=>116, 'title'=>'thinkph111p'],
//     ['id'=>115, 'title'=>'onethi22nk']
// ];
// $user->saveAll($list);

// PluginvideoappdownloadArticle::update(['title' => '2333'], ['id' => 116]);
        
        
    }
	
	
	
	//点赞
    public function add_like($request)
    {
  
        $id        = $request['id'] ;
        $uid       = $request['uid'];
        $field     = [];
        $map       = [];
        $map[]     = ["id", "=",  $id];
        $find      = $this->finds($map, $field);
        if (!$find) {
            ajaxmsg('内容已被删除', 0);
        }
        $field=["like"];
 
  
         
		//事务 
		Db::startTrans();
		try {
			$request['uid']        = $uid;
			$request['article_id'] = $id;
			$model   =  (new PluginVideoappdownloadArticleLike())->adds($request);
			//点赞成功 增加点赞次数
			if ($model) {
			        $data["like"]= $find['like'] + 1;
		 	 } else {
			        $data["like"]= $find['like'] - 1;
			 }
	         $up  = $this->updates($map, $data, $field);
		 	 Db::commit();
		 	 return $model;
		}
		catch(Exception $ex) {
			Db::rollback();
			ajaxmsg('系统错误', 401,$ex);
		}
        
        
 
        
        
     }
	//点赞
    public function add_collect($request)
    {
  
        $id        = $request['id'] ;
        $uid       = $request['uid'];
        $field     = [];
        $map       = [];
        $map[]     = ["id", "=",  $id];
        $find      = $this->finds($map, $field);
        if (!$find) {
            ajaxmsg('内容已被删除', 0);
        }
        $field=["collect"];
 
  
         
		//事务 
		Db::startTrans();
		try {
			$request['uid']        = $uid;
			$request['article_id'] = $id;
			$model   =  (new PluginVideoappdownloadArticleCollect())->adds($request);
			//点赞成功 增加点赞次数
			if ($model) {
			        $data["collect"]= $find['collect'] + 1;
		 	 } else {
			        $data["collect"]= $find['collect'] - 1;
			 } 
	         $up  = $this->updates($map, $data, $field);
		 	 Db::commit();
		 	 return $model;
		}
		catch(Exception $ex) {
			Db::rollback();
			ajaxmsg('系统错误', 401,$ex);
		}
        
        
 
        
        
     }
	
	
	public function finds($map,$field) {
	
		if ($field) {
			$finds = PluginVideoappdownloadArticle::where($map)->field($field)->findOrEmpty();
		} else {
			$finds = PluginVideoappdownloadArticle::where($map)->findOrEmpty();
		}
		if($finds->isEmpty()) {
			return '';
		}
		$finds->contents;
		return $finds;
	}
	
	public function paginate($map,$field,$order,$page) {
		
		if ($field) {
			$list = PluginVideoappdownloadArticle::with('Category')->where($map)->order($order)->field($field)->paginate($page);
		} else {
			$list = PluginVideoappdownloadArticle::with('Category')->where($map)->order($order)->paginate($page);
		}
		$data['list']=$list;
		return $data;
	}
	
	
	public function adds($request,$field) {
		unset($request['id']);
		$adds               =  new PluginVideoappdownloadArticle;
		
		$adds->save($request);
		$imgs  =   $this->getImgs($request['content']);
		$request['imgs']         =  !empty($imgs)? json_encode($imgs):'';
		$request['create_time'] =   time();
		$adds->save($request); 
		$adds->addcontents()->save(['content'=> $request['content'], 'type'=> $request['type'], 'article_id'=>$adds->id]);
		return $adds->id;
	}
 
	public function updates($map,$request,$field) {
    	
		$find = PluginVideoappdownloadArticle::where($map)->find();
		if ($find) {
			$imgs               =      isset($request['content'])?$this->getImgs($request['content']):'';
			$request['imgs']    =     !empty($imgs)? json_encode($imgs):'';
			if ($field) {
				$find->allowField($field)->save($request);
			} else {
				$find->save($request);
			}
			if (isset($request['content'])) {
				//保存关联的文章内容
				$is =   $find->addcontents->save(['content' => $request['content']]);
				return $is;
			} else {
				return true;
			}
		}
		return false;
	}
	public function up_dels($map,$request,$field) {
   
		$find = PluginVideoappdownloadArticle::where($map)->find();
		if ($find) {
			$is=	$find->save($request);
			return $is;
		}
		return false;
	}
	public function dels($map) {
    	
		$dels = PluginVideoappdownloadArticle::where($map)->find();
		if ($dels) {
			return PluginVideoappdownloadArticle::where($map)->delete();
		}
		return false;
	}
	
	/*
    * $content 文章内容
    * $order 要获取哪张图片，ALL所有图片，0第一张图片
    */
	function getImgs($content, $order = 'ALL') {
		$pattern = "/<img .*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
		preg_match_all($pattern, $content, $match);
		if (isset($match[1]) && !empty($match[1])) {
			if ($order === 'ALL') {
				return $match[1];
			}
			if (is_numeric($order) && isset($match[1][$order])) {
				return $match[1][$order];
			}
		}
		return '';
	}
	
	
	//模型方法
}